<template>
    <div class="edit-article">
      <h2>编辑文章</h2>
      <el-form :model="editPost" ref="editPostForm" class="form-container">
        <!-- 标题输入框 -->
        <div class="form-row">
          <el-form-item label="标题" :label-width="'120px'" class="form-label">
            <el-input v-model="editPost.title" placeholder="请输入文章标题" />
          </el-form-item>
        </div>
        
        <!-- 分类选择框 -->
        <div class="form-row">
          <el-form-item label="分类" :label-width="'120px'" class="form-label">
            <el-select v-model="editPost.category_id" placeholder="请选择分类">
              <el-option v-for="category in categories" :key="category.id" :label="category.name" :value="category.id" />
            </el-select>
          </el-form-item>
        </div>
        
        <!-- 内容输入框 -->
        <div class="form-row">
          <el-form-item label="内容" :label-width="'120px'" class="form-label">
            <el-input type="textarea" v-model="editPost.content" placeholder="请输入文章内容" rows="6" />
          </el-form-item>
        </div>
        
        <!-- 提交和取消按钮 -->
        <div class="form-actions">
          <el-button type="primary" @click="submitEditPost" class="submit-btn">提交修改</el-button>
          <el-button @click="cancelEdit" class="cancel-btn">取消</el-button>
        </div>
      </el-form>
    </div>
  </template>
  
  <script>
  import axios from 'axios';
  
  export default {
    data() {
      return {
        categories: [
          { id: 1, name: '技术' },
          { id: 2, name: '生活' },
          { id: 3, name: '娱乐' }
        ],
        editPost: {
          ID: null,
          title: '',
          category_id: null,
          content: ''
        }
      };
    },
    mounted() {
      this.loadArticle();
    },
    methods: {
      // 加载文章数据
      async loadArticle() {
        const articleId = this.$route.params.id; // 从路由中获取ID
        console.log('加载文章，ID:', articleId);
        try {
          const response = await axios.get(`http://localhost:9090/posts/${articleId}`);
          console.log('文章数据:', response.data.data);
          this.editPost = response.data.data; // 将返回的数据赋值给 editPost
        } catch (error) {
          console.error('加载文章失败', error);
          alert('加载文章失败');
        }
      },
  
      // 提交修改后的文章
      async submitEditPost() {
        try {
          const response = await axios.put(`http://localhost:9090/posts/${this.editPost.ID}`, this.editPost);
          if (response.status === 200) {
            alert('文章修改成功');
            this.$router.push('/article_management'); // 修改成功后跳转到文章管理页面
          } else {
            alert('修改文章失败');
          }
        } catch (error) {
          console.error('修改文章失败', error);
          alert('修改文章失败');
        }
      },
  
      // 取消编辑
      cancelEdit() {
        this.$router.push('/articles'); // 取消编辑跳转回文章管理页面
      }
    }
  };
  </script>
  
  <style scoped>
  .edit-article {
    width: 80%;
    margin: 30px auto;
    padding: 20px;
    background-color: #f9f9f9;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  }
  
  h2 {
    text-align: center;
    color: #333;
    margin-bottom: 20px;
  }
  
  .form-container {
    background-color: #fff;
    padding: 30px;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  }
  
  .form-row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
  }
  
  .form-label {
    width: 20%; /* 设置标签列宽度 */
  }
  
  .el-form-item {
    flex: 1; /* 输入框占满剩余空间 */
  }
  
  .form-actions {
    text-align: center;
    margin-top: 20px;
  }
  
  .submit-btn {
    background-color: #409eff;
    color: white;
    margin-right: 10px;
  }
  
  .cancel-btn {
    background-color: #f5f7fa;
    color: #409eff;
  }
  
  .el-input,
  .el-select {
    width: 100%;
  }
  
  .el-form-item label {
    color: #666;
  }
  </style>
  